package com.demo.web;

import com.demo.bean.PositionRequest;
import com.demo.bean.PositionVO;
import com.demo.service.AdsService;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 一个简单的Jersey资源类
 */
@Component
@Path("/")
public class SampleResource {

    private final AdsService adsService;

    /* 构造注入 */
    private SampleResource(@Autowired AdsService adsService) {
        this.adsService = adsService;
    }

    @POST
    @Path("ads")
    @Produces("application/json")
    public PositionVO getAds(@Context HttpHeaders headers, PositionRequest positionReq) {
        String authorization = headers.getHeaderString("Authorization");
        System.out.println("authorization: " + authorization);
        return adsService.getAds(positionReq);
    }

    @POST
    @Path("adList")
    @Produces("application/json")
    public List<PositionVO> getAdList(@Context HttpHeaders headers, PositionRequest positionReq) {
        String authorization = headers.getHeaderString("Authorization");
        System.out.println("authorization: " + authorization);
        PositionVO ad = adsService.getAds(positionReq);
        PositionVO vo = PositionVO.builder().title("xx").price(100.0).imageUrl("xx Image").build();
        return List.of(ad, vo);
    }

    @POST
    @Path("post")
    @Produces("application/json")
    public Map<Object, Object> post() {
        return Map.of("company", "亿贝", "location", "san jose", "employee", "10000");
    }

    @GET
    @Path("get")
    public String get() {
        return "Hello World";
    }

    /**
     * @param name path parameter不能用中文，否则400，目前还没解决
     */
    @GET
    @Path("get_with_path/{name}")
    public String pathParamGet(@PathParam("name") String name) {
        return "path param name: " + Optional.ofNullable(name).orElse("sophia");
    }

    @GET
    @Path("get_with_query")
    public String queryParamGet(@QueryParam("location") String location) {
        return "query param location: " + Optional.ofNullable(location).orElse("dalian");
    }

}
